package com.x.erp.dao;

import com.x.erp.model.Gate;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 道闸信息DAO
 */
@Repository
public class GateDao extends BaseDao {

	private final static String selectGateListSql = "SELECT T1.*, T2.MARKET_NAME FROM ERP_GATE T1, ERP_MARKET T2 WHERE T1.MARKET_ID = T2.MARKET_ID ";
	/**
	 * 查询道闸信息列表
	 * @param gate
	 * @return
	 * @throws Exception
	 */
	public List<Gate> selectGateList(Gate gate) throws Exception{
		StringBuilder sql = new StringBuilder(selectGateListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, gate);
		return this.queryForList(sql.toString(), Gate.class, params, gate);
	}

	private final static String selectGateNumSql = "SELECT COUNT(T1.GATE_ID) FROM ERP_GATE T1 WHERE 1=1 ";
	/**
	 * 查询道闸信息数量
	 * @param gate
	 * @return
	 * @throws Exception
	 */
	public int selectGateNum(Gate gate) throws Exception{
		StringBuilder sql = new StringBuilder(selectGateNumSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, gate);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param gate
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, Gate gate) {
		if(gate != null) {
			if(StringUtils.isNotEmpty(gate.getGateCode())){
				sql.append("AND T1.GATE_CODE = ? ");
				params.add(gate.getGateCode());
			}
			if(StringUtils.isNotEmpty(gate.getMarketId())){
				sql.append("AND T1.MARKET_ID = ? ");
				params.add(gate.getMarketId());
			}
			if(StringUtils.isNotEmpty(gate.getGateName())){
				sql.append("AND T1.GATE_NAME = ? ");
				params.add(gate.getGateName());
			}
		}
	}
}
